package com.xwj.nio.netty.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;

import java.io.UnsupportedEncodingException;

/**
 * @author xwj
 * @date 2020/6/23
 */
public class DelimiterBasedFrameDecoderTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        ByteBuf delimiter = Unpooled.copiedBuffer("\t".getBytes("UTF-8"));
        // 创建嵌入式通道
        EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel ch) {
                ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, true, delimiter));
                ch.pipeline().addLast(new io.netty.handler.codec.string.StringDecoder());
                ch.pipeline().addLast(new StringProcessHandler());
            }
        });

        for (int i = 0; i < 100; i++) {
            ByteBuf buf = Unpooled.buffer();
            buf.writeBytes("海内存知己天涯若比邻".getBytes("UTF-8"));
            buf.writeBytes("\t".getBytes("UTF-8"));
            channel.writeInbound(buf);
        }
    }
}
